package com.todolist.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.todolist.client.event.DeleteProjectEvent;
import com.todolist.client.event.DeleteProjectEventHandler;
import com.todolist.client.event.RefreshProjectsEvent;
import com.todolist.client.event.RefreshProjectsEventHandler;
import com.todolist.client.service.ProjectService;
import com.todolist.client.service.ProjectServiceAsync;
import com.todolist.shared.domain.Project;

import java.util.ArrayList;

public class ToDoListPageWidget extends Composite {
    private static final String ADD_TODOLIST_BUTTON_IMAGE = "addToDoListButton.png";
    private static final String BUTTON_STYLE = "button";

    interface ToDoListPageWidgetUiBinder extends UiBinder<Widget, ToDoListPageWidget> {
    }

    private static ToDoListPageWidgetUiBinder uiBinder = GWT.create(ToDoListPageWidgetUiBinder.class);

    private ProjectServiceAsync projectService = GWT.create(ProjectService.class);

    private SimpleEventBus eventBus = null;

    @UiField(provided = true)
    PushButton addToDoListButton;

    @UiField
    VerticalPanel projectsPanel;

    public ToDoListPageWidget(final SimpleEventBus eventBus) {
        this.eventBus = eventBus;

        initProvidedWidgets();
        initWidget(uiBinder.createAndBindUi(this));
        initHandlers();
    }

    @UiHandler("addToDoListButton")
    void handleAddToDoListButtonClick(ClickEvent clickEvent) {
        ToDoListGridWidget toDoListGridWidget = new ToDoListGridWidget(eventBus);
        toDoListGridWidget.projectNameTextBox.setEnabled(true);
        projectsPanel.add(toDoListGridWidget);
    }

    private void initHandlers() {
        eventBus.addHandler(RefreshProjectsEvent.TYPE, new RefreshProjectsEventHandler() {
            @Override
            public void refreshProjects() {
                refreshProjectsPanelData();
            }
        });

        eventBus.addHandler(DeleteProjectEvent.TYPE, new DeleteProjectEventHandler() {
            @Override
            public void deleteProject(ToDoListGridWidget toDoListGridWidget) {
                projectsPanel.remove(toDoListGridWidget);
            }
        });
    }

    public void refreshProjectsPanelData() {
        projectsPanel.clear();

        projectService.getProjects(new AsyncCallback<ArrayList<Project>>() {
            @Override
            public void onFailure(Throwable throwable) {
                Window.alert("Cannot get the projects!");
            }

            @Override
            public void onSuccess(ArrayList<Project> result) {
                for (Project project : result) {
                    ToDoListGridWidget toDoListGridWidget = new ToDoListGridWidget(eventBus, project);

                    projectsPanel.add(toDoListGridWidget);
                }
            }
        });
    }

    private void initProvidedWidgets() {
        addToDoListButton = new PushButton(new Image(ADD_TODOLIST_BUTTON_IMAGE));
        addToDoListButton.setStyleName(BUTTON_STYLE);
    }
}
